#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>

/*int main(int argv, char* argc[])
{
	pid_t pid;
	pid = fork();
	if(pid < 0)
	{
		perror("fork");
	}else if(pid == 0)
	{
		printf("pid = %d ppid = %d",getpid(),getppid());
		printf("child\n");
		sleep(25);
		printf("pid = %d ppid = %d",getpid(),getppid());

	}else
	{
		exit(0);
	}
	return 0;
}*/

//========================================


//守护进程
//1.
/*int main()
{

	//创建子进,退夫
	pid_t pid;
	if(fork()>0)
	{
		exit(0);
	}

	return 0;
}*/

//==============================

/*
//2.子进程创建新会话
int main()
{
	pid_t pid;
	pid_t pit;
	pid = fork();
	if(pid < 0)
	{
		perror("fork");
	}

	pit = setsid();
	if(pid < 0)
	{
		perror("setsid");
		exit(0);
	}else 
	{
		printf("会话 = %d pid = %d 进程 = %d\n",getsid(pid),getpid(),getpgid(pid));
	}
}
*/

//======================================================

int main()
{
	//创建进程
	pid_t pid;
	pid = fork();
	if(pid < 0)
	{
		perror("fork");
		return 0;
	}else if(pid == 0)
	{
		printf("pid = %d ppid =%d\n", getpid(), getppid());
	
		//更改会话组长
		if(setsid() < 0)
		{
			exit(0);
		}else
		{
			printf("会话 = %d pid = %d 进程 = %d 进程组= %d\n",getsid(pid),getpid(),getppid(), getpgid(pid));
		}

		//重设文件权限淹码
		//为0
		if(umask(0) < 0)
		{
			exit(-1);
		}

		//关闭三个系统文件符
		//stdin、 stdout、 stderr
		close(0);
		close(1);
		close(2);
	}else
	{
		exit(0);
	}
}





























